package net.schst.EventDispatcher.tests;

import net.schst.EventDispatcher.Event;
import net.schst.EventDispatcher.EventDispatcher;
import net.schst.EventDispatcher.tests.helpers.CaptureEventListener;
import junit.framework.TestCase;

public class EventQueueTestCase extends TestCase {

	/*
	 * Test method for 'net.schst.EventDispatcher.EventDispatcher.triggerEvent(Event, boolean)'
	 */
	public void testTriggerEventObject() {
		EventDispatcher dispatcher = EventDispatcher.getDetachedInstance();
		
		Event triggered = new Event("onTest");
		dispatcher.triggerEvent(triggered, true);
		
		// With local listener
		CaptureEventListener listener = new CaptureEventListener();
		dispatcher.addListener("onTest", listener);		
		Event captured = listener.getLastCapturedEvent();		
		TestCase.assertSame(triggered, captured);
		
		// With global listener
		CaptureEventListener listener2 = new CaptureEventListener();
		dispatcher.addGlobalListener(listener2);
		Event capturedByGlobal = listener.getLastCapturedEvent();		
		TestCase.assertSame(triggered, capturedByGlobal);		
	}

	/*
	 * Test method for 'net.schst.EventDispatcher.EventDispatcher.triggerEvent(String, boolean)'
	 */
	public void testTriggerEventString() {
		EventDispatcher dispatcher = EventDispatcher.getDetachedInstance();
		
		Event triggered = dispatcher.triggerEvent("onTest", true);
		
		// With local listener
		CaptureEventListener listener = new CaptureEventListener();
		dispatcher.addListener("onTest", listener);		
		Event captured = listener.getLastCapturedEvent();
		TestCase.assertSame(triggered, captured);
		
		// With global listener
		CaptureEventListener listener2 = new CaptureEventListener();
		dispatcher.addGlobalListener(listener2);
		Event capturedByGlobal = listener.getLastCapturedEvent();		
		TestCase.assertSame(triggered, capturedByGlobal);
		
	}
}